package com.cy.jt.security.interceptor;

import com.cy.jt.security.util.JwtUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 令牌(token:ticker-通票)拦截器
 * 其中,handlerInterceptor为SpringMVC中的拦截器
 *
 */
public class TokenInterceptor implements HandlerInterceptor {
    /**
     * prehandle 在目标Controller方法执行之前执行
     */
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        System.out.println("==preHandler==");
        String token = request.getParameter("Authentication");
//        String token = request.getHeader("Authentication");
        System.out.println("token="+token);
        if (token==null||"".equals(token))
            throw new RuntimeException("please login");
        boolean flag = JwtUtils.isTokenExpired(token);
        if (flag)
            throw new RuntimeException("login timeout");
        return true;//true表示放行,false表示拦截到请求以后,不再继续传递
    }


}
